Android Parcelable数据序列化详解

什么是什么是Parcelable

Parcelable是Android sdk提供的用实现于数据序列化的一个接口,不同于Java中的基于磁盘或者网络的Serializable,Parcelable是基于内存的,由于内存的读写速度高于磁盘,因此在Android中跨进程对象传递一般使用Parcelable。

如何使用Parcelable

要想使用Parcelable并不容易,需要编写很多代码,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.itfitness.androidparcelabletest;

import android.os.Parcel;
import android.os.Parcelable;

public class BookBean implements Parcelable {
private int bookId;
private String bookName;
private String bookAuthor;
private String bookPrice;

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.bookId);
dest.writeString(this.bookName);
dest.writeString(this.bookAuthor);
dest.writeString(this.bookPrice);
}

public BookBean() {
}

protected BookBean(Parcel in) {
this.bookId = in.readInt();
this.bookName = in.readString();
this.bookAuthor = in.readString();
this.bookPrice = in.readString();
}

public static final Parcelable.Creator<BookBean> CREATOR = new Parcelable.Creator<BookBean>() {
@Override
public BookBean createFromParcel(Parcel source) {
return new BookBean(source);
}

@Override
public BookBean[] newArray(int size) {
return new BookBean[size];
}
};
}

Parcelable虽然好用但并不好使用,每次使用需要编写这么多代码显然很麻烦,但是因为我们使用的是Android Studio啊,Android Studio中有一个专门用来生成Parcelable代码的插件:Android Parcelable code generator,如下图所示。

安装完成后重启Android Studio然后编写一个BookBean类,按Alt+Insert会弹出一个窗口。

选择Parcelable选项,Android Studio会自动生成代码,实现Parcelable接口。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.itfitness.androidparcelabletest;

import android.os.Parcel;
import android.os.Parcelable;

public class BookBean implements Parcelable {
private int bookId;
private String bookName;
private String bookAuthor;
private String bookPrice;

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.bookId);
dest.writeString(this.bookName);
dest.writeString(this.bookAuthor);
dest.writeString(this.bookPrice);
}

public BookBean() {
}

protected BookBean(Parcel in) {
this.bookId = in.readInt();
this.bookName = in.readString();
this.bookAuthor = in.readString();
this.bookPrice = in.readString();
}

public static final Parcelable.Creator<BookBean> CREATOR = new Parcelable.Creator<BookBean>() {
@Override
public BookBean createFromParcel(Parcel source) {
return new BookBean(source);
}

@Override
public BookBean[] newArray(int size) {
return new BookBean[size];
}
};
}

Activity中的传值

MainActivity
1
2
3
4
5
6
7
8
9
10
11
12
13
final BookBean bookBean=new BookBean();
bookBean.setBookId(1);
bookBean.setBookAuthor("阿萨德");
bookBean.setBookName("水浒传");
bookBean.setBookPrice("¥24");
findViewById(R.id.bt_test).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, TestActivity.class);
intent.putExtra("testData",bookBean);
startActivity(intent);
}
});
TestActicity
1
2
BookBean bookBean = getIntent().getParcelableExtra("testData");
Log.e("测试",bookBean.getBookId()+"==="+bookBean.getBookAuthor()+"==="+bookBean.getBookName()+"==="+bookBean.getBookPrice());

坚持原创技术分享,您的支持将鼓励我继续创作!
-------------本文结束感谢您的阅读-------------